//
//  MessageViewController.m
//  搭建框架(代码已封装)
//
//  Created by My Mac on 2017/1/25.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "MessageViewController.h"
#import "LeftMenuView.h"
#import "MenuView.h"
#import "ChatViewController.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)MenuView *menu;
@property(nonatomic,strong) UITableView *messageTableView;

@property (strong,nonatomic)NSMutableArray *timeArray;
@property (strong,nonatomic)UIRefreshControl *refresh;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initSwipe];
    
    [self initTableView];
    
    [self setBeginRefreshing];
}

-(void)initTableView
{
    _messageTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _messageTableView.delegate = self;
    _messageTableView.dataSource = self;
    _messageTableView.tableFooterView = [[UIView alloc]init];
    self.tableView = _messageTableView;
}

-(void)setBeginRefreshing
{
    _timeArray = [NSMutableArray array];
    //初始化刷新
    _refresh = [[UIRefreshControl alloc]init];
    //刷新图标的颜色
    _refresh.tintColor = [UIColor redColor];
    //标题
    _refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [_refresh addTarget:self action:@selector(refreshTableViewAction:) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = _refresh;
    self.refreshControl.backgroundColor = [UIColor greenColor];
    self.refreshControl.alpha = 0.5;
}
-(void)refreshTableViewAction:(UIRefreshControl *)refresh
{
    if(refresh.refreshing){
        refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
        [self performSelector:@selector(refreshData) withObject:nil afterDelay:1];
    }
}
-(void)refreshData
{
    NSDateFormatter * dataFormatter = [[NSDateFormatter alloc]init];
    [dataFormatter setDateFormat:@"HH:mm"];
    NSString *systemTime = [dataFormatter stringFromDate:[NSDate date]];
    NSString *lastUpdataTime = [NSString stringWithFormat:@"上一次刷新:%@",systemTime];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastUpdataTime];
    
    [_timeArray addObject:lastUpdataTime];
    [self.refreshControl endRefreshing];
    [_messageTableView reloadData];
}

-(void)initSwipe
{
    //创建轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:_messageTableView action:@selector(handleSwipe:)];
    //设置该手势处理器处理几个手指的轻扫手势（默认为1）
    swipe.numberOfTouchesRequired = 1;
    //设置该手势的处理方向
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    //为视图添加手势
    [_messageTableView addGestureRecognizer:swipe];
}

-(void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        //设置弹出视图的大小
        LeftMenuView *demo = [[LeftMenuView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width * 0.66, [[UIScreen mainScreen] bounds].size.height)];
        //协议的代理者
        //菜单视图与视图的依赖
        MenuView *menu = [MenuView MenuViewWithDependencyView:_messageTableView MenuView:demo isShowCoverView:YES];
        self.menu = menu;
        [self.menu show];
    }
}

#pragma mark -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _timeArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _timeArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%lu",indexPath.row);
    ChatViewController *chatView = [[ChatViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:chatView];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
